package com.cornergj.back.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cornergj.model.Admin;
import com.cornergj.model.Entity;
import com.cornergj.model.Entity.AdminCriteria;
import com.cornergj.model.Entity.FrontImgCriteria;
import com.cornergj.model.Entity.Pagination;
import com.cornergj.model.Entity.ServiceContentCriteria;
import com.cornergj.model.Entity.SiteOfCityCriteria;
import com.cornergj.model.Entity.SiteOfProvinceCriteria;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.FrontImg;
import com.cornergj.model.ServiceContent;
import com.cornergj.model.SiteOfCity;
import com.cornergj.model.SiteOfProvince;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.AdminService;
import com.cornergj.service.FrontImgService;
import com.cornergj.service.ServiceContentService;
import com.cornergj.service.SiteOfCityService;
import com.cornergj.service.SiteOfProvinceService;
import com.cornergj.uitl.FileUtils;


/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年1月31日
 */
@Controller
@RequestMapping("/site")
public class SiteController {
	
	@Autowired
	private SiteOfCityService siteOfCityService;
	@Autowired
	private SiteOfProvinceService siteOfProvinceService;
	
	/**
	 * 分页方法
	 * @param request
	 * @param bean
	 * @param pagination
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(HttpServletRequest request, SiteOfProvince bean, Pagination pagination) {
		//查询条件封装
		SiteOfProvinceCriteria criteria = new SiteOfProvinceCriteria();
		//分页查询
		Pagination list = siteOfProvinceService.selectPage(criteria, pagination);
		
		request.setAttribute("pagination", list);
		request.setAttribute("bean", bean);
		return "site/list";
	}
	
	/**
	 * 编辑跳转
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/citylist", method = RequestMethod.GET)
	public String list(HttpServletRequest request, SiteOfCity bean, Pagination pagination,int siteProvinceid) {
		//查询条件封装
		SiteOfCityCriteria criteria = new SiteOfCityCriteria();
		//分页查询
		criteria.setProvinceId(Value.eq(siteProvinceid));
		Pagination list = siteOfCityService.selectPage(criteria, pagination);
		
		request.setAttribute("pagination", list);
		request.setAttribute("siteProvinceid", siteProvinceid);
		request.setAttribute("bean", bean);
		return "site/citylist";
	}
	/**
	 * 添加
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult save(HttpServletRequest request,String provincesite,String citysite) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		try{
			SiteOfProvinceCriteria criteria=new SiteOfProvinceCriteria();
			criteria.setProvince(Value.eq(provincesite));
			SiteOfProvince getprovince=siteOfProvinceService.selectOne(criteria);
			if(getprovince!=null){
				int pid=getprovince.getId();
				SiteOfCityCriteria citycriteria=new SiteOfCityCriteria();
				citycriteria.setProvinceId(Value.eq(pid));
				List<SiteOfCity> getsiteOfCitylist=siteOfCityService.selectList(citycriteria);
				if(getsiteOfCitylist!=null){
					for(SiteOfCity csite:getsiteOfCitylist){
						if(csite.getCity().equals(citysite)){
							jsonResult.addErrorCode(ErrorCode.SAVE_SITE_FAILE);
							List<ErrorCode> errorlist=new ArrayList();
							errorlist.add(ErrorCode.SAVE_SITE_FAILE);
							jsonResult.setMessage(errorlist);
						}else{
							SiteOfCity pushsiteOfCity=new SiteOfCity();
							pushsiteOfCity.setCity(citysite);
							pushsiteOfCity.setProvinceId(pid);
							siteOfCityService.save(pushsiteOfCity);
						}
					}
				}
			}else{
				SiteOfProvince siteOfProvince=new SiteOfProvince();
				SiteOfCity siteOfCity=new SiteOfCity();
				siteOfProvince.setProvince(provincesite);
				
				int provinceId=siteOfProvinceService.save(siteOfProvince);
				siteOfCity.setCity(citysite);
				siteOfCity.setProvinceId(provinceId);
				siteOfCityService.save(siteOfCity);
			}
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.SYS_ERR);
		}
		return jsonResult;
	}
	/**
	 * 删除
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult delete(HttpServletRequest request,int siteid) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		try{
			siteOfCityService.delete(siteid);
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.SYS_ERR);
		}
		return jsonResult;
	}
	/**
	 * 删除
	 * @param request
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping(value = "/provincedel", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult provincedel(HttpServletRequest request,int provincesiteid) throws IllegalStateException, IOException {
		JsonResult jsonResult=new JsonResult();
		try{
			SiteOfCityCriteria criteria = new SiteOfCityCriteria();
			criteria.setProvinceId(Value.eq(provincesiteid));
			siteOfCityService.delete(criteria);
			
			siteOfProvinceService.delete(provincesiteid);
		}catch(Exception e){
			e.printStackTrace();
			jsonResult.addErrorCode(ErrorCode.SYS_ERR);
		}
		return jsonResult;
	}
}
